/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import controller.UserManager;
import entity.User;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

/**
 *
 * @author xubuntu
 */
@ManagedBean    
@ViewScoped
public class SearchBean {

    @ManagedProperty( value = "#{loginBean}" )
    private LoginBean wlmPage;
    @ManagedProperty( value = "#{personalPageBean}" )
    private PersonalPageBean personalPageBean;
    @EJB
    private UserManager usrMgr;
    @ManagedProperty( value = "#{navigationBean}" )
    private NavigationBean navigationBean;
    
    private String name;
    private List<User> possibleFriends;
    
    private User amicoSelected;

  
    public SearchBean() {
        name = "";
        amicoSelected = new User();
        possibleFriends = new ArrayList<>();
    }
    
    public User getAmicoSelected() {
        return amicoSelected;
    }

    public void setAmicoSelected(User amicoSelected) {
        this.amicoSelected = amicoSelected;
    }

    public List<User> getPossibleFriends() {
        return possibleFriends;
    }

    public String getName(){
            return this.name;
    }
 
    public void setName(String name){
            this.name = name;
    }

    public void search(){

        List<User> temp;
        temp= usrMgr.getUsersFind(name);
        
        int i = 0;
        while( i < temp.size() )
        {
            if( temp.get( i ).getNickname().equals(wlmPage.getUser().getNickname())==true)
                temp.remove( i );
            else
                i++;
        }
       possibleFriends= temp;
    }
    /*
    whereToGo usata quando si clicca su un amico per vedere il suo calendario eventi:
    se l'amico ha settato il proprio calendario come pubblico, vieni reindirizzato
    alla sua pagina di calendario eventi, 
    se è settato come privato vieni reindirizzato ad una pagina che ti informa di questo
    */
    public String whereToGo(){
        personalPageBean.setUserSelected(amicoSelected);
        possibleFriends.clear();
        if(amicoSelected.getPublicCal()==0)
            return navigationBean.redirectToPrivatePagePage();
        return navigationBean.redirectToCalendarPage();
    }
    
    public LoginBean getWlmPage() {
        return wlmPage;
    }

    public void setWlmPage(LoginBean wlmPage) {
        this.wlmPage = wlmPage;
    }
    
    public NavigationBean getNavigationBean() {
        return navigationBean;
    }

    public void setNavigationBean(NavigationBean navigationBean) {
        this.navigationBean = navigationBean;
    }

    public PersonalPageBean getPersonalPageBean() {
        return personalPageBean;
    }

    public void setPersonalPageBean(PersonalPageBean personalPageBean) {
        this.personalPageBean = personalPageBean;
    }


    
}
